package com.travel.rabbit.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.travel.rabbit.dao.GoodsCategoryDao;
import com.travel.rabbit.entity.GoodsCategory;
import com.travel.rabbit.enums.BusinessEnum;
import com.travel.rabbit.enums.DeleteEnum;
import com.travel.rabbit.service.GoodsCategoryService;
import com.travel.rabbit.entity.Result;
import com.travel.rabbit.utils.StringCustomizedUtils;
import com.travel.rabbit.dto.GoodsCategoryDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author 云澜
 * @date 2021/12/31 3:59 下午
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    GoodsCategoryDao categoryDao;

    @Override
    public Result add(GoodsCategoryDTO categoryVO) {
        GoodsCategory checkNameExist = categoryDao.selectOne(Wrappers.<GoodsCategory>lambdaQuery()
                .eq(GoodsCategory::getCategoryName, categoryVO.getCategoryName())
                .eq(GoodsCategory::getIsDelete, DeleteEnum.NORMAL.getCode()));
        if (!Objects.isNull(checkNameExist)){
            return Result.error(BusinessEnum.CATEGORY_EXIST.getCode(),BusinessEnum.CATEGORY_EXIST.getMsg());
        }
        GoodsCategory info = new GoodsCategory();
        BeanCopier beanCopier = BeanCopier.create(GoodsCategoryDTO.class,GoodsCategory.class,false);
        beanCopier.copy(categoryVO,info,null);
        if (StringUtils.isBlank(categoryVO.getParentCode()) || "0".equals(categoryVO.getParentCode())){
            info.setParentCode("0");
        }
        switch (categoryVO.getLevel()){
            case 0:
                info.setCategoryCode(queryNext(0));
                break;
            case 1:
                info.setCategoryCode(queryNext(1));
                break;
            case 2:
                info.setCategoryCode(queryNext(2));
            case 3:
                info.setCategoryCode(queryNext(3));
            default:
                break;
        }
        info.setIsDelete(0);
        info.setCreateBy(categoryVO.getOptBy());
        info.setUpdateBy(categoryVO.getOptBy());
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        int insert = categoryDao.insert(info);
        return Result.success();
    }

    private String queryNext(int level){
        String result = categoryDao.queryNext(level);
        if (StringUtils.isBlank(result)){
            switch (level){
                case 0:
                    result = "1";
                    break;
                case 1:
                    result = "10000";
                    break;
                case 2:
                    result = "50000";
                    break;
                case 3:
                    result = "100000";
                    break;
                default:
                    break;
            }
        }
        return StringCustomizedUtils.replaceBlank(result) ;
    }

    @Override
    public GoodsCategory queryById(Integer id) {
        return null;
    }
}
